package es.titoapps.spanishhandball.configuration;

import java.util.ArrayList;
import java.util.List;

import es.titoapps.spanishhandball.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class Configuration {

	private static Configuration configuration=null;
	private static Context context;
	
	private static boolean loadImages=true;//0 false 1 true
	private static int maxNumberOfNews,updateIntervalInMinutes,numberOfNewsToShow;
	
	
	public static void loadConfiguration(Context contexto){
		
		if(configuration==null){
			configuration=new Configuration();
			context=contexto;
			configuration.updateConfiguration();
		}
	}
	
	
	public static Configuration getConfiguration(){
		
		return configuration;
	}
	
	/**
	 * Carga de Imagenes
	 * @return
	 */
	public boolean shouldLoadImages(){

		return loadImages;
	}
	
	public static int getUpdateIntervalInMinutes() {
		return updateIntervalInMinutes;
	}


	public static int getMaxNumberOfNews() {
		return maxNumberOfNews;
	}

	public static int getNumberOfNewsToShow() {
		return numberOfNewsToShow;
	}
	/**
	 * Sources Configuration
	 */
	public List<String> getNewsSources(){
		
		PreferenceManager.setDefaultValues(context, R.xml.sourcesconfiguration, false);
	
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		
		List<String> newsSelected= new ArrayList<String>();
		
		for(String source: NewsSources.getValues()){
			
			if(pref.getBoolean(source,true))
				newsSelected.add(source);
		}
					
		return newsSelected;
		
	}
	
	public List<String> getBlogSources(){
		
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		List<String> blogsSelected= new ArrayList<String>();
		
		for(String source: BlogSources.getValues()){
			
			if(pref.getBoolean(source,true))
				blogsSelected.add(source);
		}

		return blogsSelected;
	}
	
	/**
	 * Update Preferences
	 * @param bundle
	 */
	public void updateConfiguration(){
		
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		loadImages=pref.getBoolean(context.getString(R.string.configLoadImagesKey),true);
		maxNumberOfNews=Integer.parseInt(pref.getString(context.getString(R.string.configMaxNewsKey),"100"));
		updateIntervalInMinutes=Integer.parseInt(pref.getString(context.getString(R.string.configUpdateIntervalKey),"30"));
		numberOfNewsToShow=Integer.parseInt(pref.getString(context.getString(R.string.configNumberOfNewsToShowKey),"30"));
	}
}
